package com.workpaper.app.plan.view.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;

import com.workpaper.app.cache.resource.ViewInfoCache;
import com.workpaper.app.consts.common.CommonViewConsts;
import com.workpaper.app.plan.view.listener.DeleteDailyPlanInfoListener;
import com.workpaper.app.plan.view.listener.EditDailyPlanInfoListener;
import com.workpaper.app.plan.view.listener.ShowDailyPlanInfoListener;
/**
 * 为TableViewer中的行设置按钮
 * @author zhouyujun
 */
public class DailyPlanManageViewUtils {
	
	private HashMap<Table, List<TableEditor>>  editorMap = new HashMap<Table, List<TableEditor>>();
	
	private static class Holder{
		private static DailyPlanManageViewUtils INSTANCE = new DailyPlanManageViewUtils();
	}
	
	
	private DailyPlanManageViewUtils() {
		super();
	}
	
	public static DailyPlanManageViewUtils getInstance() {
		return Holder.INSTANCE;
	}

	/**
	 * 设置行操作按钮：
	 * （1）先从缓存中清理掉TableViewer之前的TableEditor及按钮
	 * （2）为table的每一行添加新的TableEditor和Button
	 * （3）缓存
	 * @param tableViewer
	 */
	public void addRowOperatingButtons(Table table) {
		clearRowButtons(table);
		
		List<TableEditor> editorList = new ArrayList<TableEditor>();
		TableItem[] items = table.getItems();
		for (TableItem item : items) {
			addShowInfoBtn(table, editorList, item);
			
			addEditButton(table, editorList, item);
			
			addDeleteButton(table, editorList, item);
		}
		editorMap.put(table, editorList);
	}
	
	/**
	 * 清理每一行的操作按钮
	 * @param table
	 */
	public void clearRowButtons(Table table) {
		List<TableEditor> oldList = editorMap.get(table);
		if(oldList != null) {
			//清理之前的按钮
			for (TableEditor tableEditor : oldList) {
				Control control = tableEditor.getEditor();	//销毁按钮避免再次创建按钮的时候，通过setData()设置的数据混乱
				control.dispose();
				tableEditor.dispose();
			}
			oldList.clear();
		}
	}
	
	
	
	/**
	 * 删除按钮
	 * @param tableViewer
	 * @param editorList
	 * @param item
	 */
	private void addDeleteButton(Table table, List<TableEditor> editorList, TableItem item) {
		TableEditor delBtnEditor = new TableEditor(table);
		Button delBtn = new Button(table, SWT.BUTTON2);
		delBtn.setText(ViewInfoCache.get(CommonViewConsts.ButtonText_Delete));
		String id = (String) item.getData();
		delBtn.setData(id);
		delBtn.addSelectionListener(new DeleteDailyPlanInfoListener());
		delBtn.pack();
		delBtnEditor.minimumWidth = delBtn.getSize().x;	//Editor的宽度 = Button的宽度
		delBtnEditor.minimumHeight = delBtn.getSize().y;	//Editor的高度 = Button的高度
		delBtnEditor.horizontalAlignment = SWT.RIGHT;	//右侧
		delBtnEditor.setEditor(delBtn, item, table.getColumnCount() - 1);	
		editorList.add(delBtnEditor);
	}
	/**
	 * 编辑按钮
	 * @param tableViewer
	 * @param editorList
	 * @param item
	 */
	private void addEditButton(Table table, List<TableEditor> editorList, TableItem item) {
		TableEditor editBtnEditor = new TableEditor(table);
		Button editBtn = new Button(table, SWT.BUTTON2);
		editBtn.setText(ViewInfoCache.get(CommonViewConsts.ButtonText_Edit));
		String id = (String) item.getData();
		editBtn.setData(id);
		editBtn.addSelectionListener(new EditDailyPlanInfoListener());
		editBtn.pack();
		editBtnEditor.minimumWidth = editBtn.getSize().x;	//Editor的宽度 = Button的宽度
		editBtnEditor.minimumHeight = editBtn.getSize().y;	//Editor的高度 = Button的高度
		editBtnEditor.horizontalAlignment = SWT.CENTER;	//居中
		editBtnEditor.setEditor(editBtn, item, table.getColumnCount() - 1);	
		editorList.add(editBtnEditor);
	}
	
	
	/**
	 * 新增查看按钮
	 * @param tableViewer
	 * @param editorList
	 * @param item
	 */
	private void addShowInfoBtn(Table table, List<TableEditor> editorList, TableItem item) {
		TableEditor showBtnEditor = new TableEditor(table);
		Button showInfoBtn = new Button(table, SWT.BUTTON2);
		showInfoBtn.setText(ViewInfoCache.get(CommonViewConsts.ButtonText_Show));
		String id = (String) item.getData();
		showInfoBtn.setData(id);
		showInfoBtn.addSelectionListener(new ShowDailyPlanInfoListener());
		showInfoBtn.pack();
		showBtnEditor.minimumWidth = showInfoBtn.getSize().x;	//Editor的宽度 = Button的宽度
		showBtnEditor.minimumHeight = showInfoBtn.getSize().y;	//Editor的高度 = Button的高度
		showBtnEditor.horizontalAlignment = SWT.LEFT;	//左侧
		showBtnEditor.setEditor(showInfoBtn, item, table.getColumnCount() - 1);	
		editorList.add(showBtnEditor);
	}
}
